﻿using HuiZe.education.View;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;

namespace Paint
{
    public class InkCanvasToolViewModel :  NotifyPropertyChanged
    {
        private const string FontSmall = "小";
        private const string FontMiddle = "中";
        private const string FontLarge = "大";

        private Color selectedAccentColor;
        private SolidColorBrush selectedBrushColor;
        private string selectedFontSize;

        private Color[] accentColors = new Color[]{
            Color.FromRgb(0x00, 0x00, 0x00),   // black
            Color.FromRgb(0xa4, 0xc4, 0x00),   // lime
            Color.FromRgb(0x60, 0xa9, 0x17),   // green
            Color.FromRgb(0x00, 0x8a, 0x00),   // emerald
            Color.FromRgb(0x00, 0xab, 0xa9),   // teal
            Color.FromRgb(0x1b, 0xa1, 0xe2),   // cyan
            Color.FromRgb(0x00, 0x50, 0xef),   // cobalt
            Color.FromRgb(0x6a, 0x00, 0xff),   // indigo
            Color.FromRgb(0xaa, 0x00, 0xff),   // violet
            Color.FromRgb(0xf4, 0x72, 0xd0),   // pink
            Color.FromRgb(0xd8, 0x00, 0x73),   // magenta
            Color.FromRgb(0xa2, 0x00, 0x25),   // crimson
            Color.FromRgb(0xe5, 0x14, 0x00),   // red
            Color.FromRgb(0xfa, 0x68, 0x00),   // orange
            Color.FromRgb(0xf0, 0xa3, 0x0a),   // amber
            Color.FromRgb(0xe3, 0xc8, 0x00),   // yellow
            Color.FromRgb(0x82, 0x5a, 0x2c),   // brown
            Color.FromRgb(0x6d, 0x87, 0x64),   // olive
            Color.FromRgb(0x64, 0x76, 0x87),   // steel
            Color.FromRgb(0x76, 0x60, 0x8a),   // mauve
            Color.FromRgb(0x87, 0x79, 0x4e),   // taupe
        };

        public InkCanvasToolViewModel()
        {
            if (AppearanceManager.Current.FontSize == FontSize.Small)
                this.SelectedFontSize = FontSmall;
            else if (AppearanceManager.Current.FontSize == FontSize.Middle)
                this.SelectedFontSize = FontMiddle;
            else if (AppearanceManager.Current.FontSize == FontSize.Large)
                this.SelectedFontSize = FontLarge;

            SyncThemeAndColor();
            SyncFontSize();

            AppearanceManager.Current.PropertyChanged += OnAppearanceManagerPropertyChanged;
        }

        private void SyncThemeAndColor()
        {
            // and make sure accent color is up-to-date
            this.SelectedAccentColor = AppearanceManager.Current.AccentColor;

        }

        private void SyncFontSize()
        {
            if (AppearanceManager.Current.FontSize == FontSize.Small)
                this.SelectedFontSize = FontSmall;
            else if (AppearanceManager.Current.FontSize == FontSize.Middle)
                this.SelectedFontSize = FontMiddle;
            else if (AppearanceManager.Current.FontSize == FontSize.Large)
                this.SelectedFontSize = FontLarge;
        }

        private void OnAppearanceManagerPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "ThemeSource" || e.PropertyName == "AccentColor")
            {
                SyncThemeAndColor();
                SyncFontSize();
            }
        }

        public string[] FontSizes
        {
            get { return new string[] { FontSmall, FontMiddle, FontLarge }; }
        }

        public Color[] AccentColors
        {
            get { return this.accentColors; }
        }

        public string SelectedFontSize
        {
            get { return this.selectedFontSize; }
            set
            {
                if (this.selectedFontSize != value)
                {
                    this.selectedFontSize = value;
                    OnPropertyChanged("SelectedFontSize");

                    if(value== "小")
                        BeginClass27.settings.StrokeThickness = 1;
                    else if (value == "中")
                        BeginClass27.settings.StrokeThickness = 5;
                    else if (value == "大")
                        BeginClass27.settings.StrokeThickness = 9;
                }
            }
        }

        public Color SelectedAccentColor
        {
            get { return this.selectedAccentColor; }
            set
            {
                if (this.selectedAccentColor != value)
                {
                    this.selectedAccentColor = value;
                    OnPropertyChanged("SelectedAccentColor");

                    AppearanceManager.Current.AccentColor = value;

                    SolidColorBrush br = new SolidColorBrush(Color.FromArgb(value.A, value.R, value.G, value.B));
                    SelectedBrushColor = br;
                    BeginClass27.settings.Color = br;
                }
            }
        }

        public SolidColorBrush SelectedBrushColor
        {
            get { return this.selectedBrushColor; }
            set
            {
                if (this.selectedBrushColor != value)
                {
                    this.selectedBrushColor = value;
                    OnPropertyChanged("SelectedBrushColor");
                }
            }
        }

    }
}
